package com.imay.live.adapter.holder;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.fragment.dynamics.OnTouchHeartListener;
import com.imay.live.helper.LevelHelper;
import com.imay.live.helper.LocationHelper;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.model.UserInfoModel;
import com.imay.live.model.UserModel;
import com.imay.live.utils.AppUtil;
import com.imay.live.utils.GlideUtil;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by chan on 2017/4/6.
 */

public class LiveViewHolder {
    private boolean visiableFeed;//是否暂时关注按钮
    private LocationHelper locationHelper;
    long myuid;

    public LiveViewHolder(boolean visiableFeed) {
        this.visiableFeed = visiableFeed;
        locationHelper = LocationHelper.getInstance();
        UserInfoModel userInfoModel = UserInfoCache.getUserInfo();
        if (userInfoModel != null && userInfoModel.getUser() != null) {
            myuid = userInfoModel.getUser().getUid();
        }
    }

    public void convert(final BaseViewHolder helper, final RoomInfoModel item, final OnTouchHeartListener listener) {
        Context mContext = helper.getConvertView().getContext().getApplicationContext();

        // VIP的显示图标
        int vip = 0;
        if (item.getUser() != null) {
            vip = item.getUser().getVip();
        }
        int icon = LevelHelper.getVIPIcon(vip);
        if (icon == 0) {
            helper.setVisible(R.id.iv_item_content_vip, false);
        } else {
            helper.setVisible(R.id.iv_item_content_vip, true);
            helper.setImageResource(R.id.iv_item_content_vip, icon);
        }

        helper.setVisible(R.id.tv_item_content_live_num, true);
        helper.setText(R.id.tv_item_content_live_num, mContext.getString(R.string.str_dynamics_live_text, item.getCountUser()));

        // 图片
        if (TextUtils.isEmpty(item.getImgFace())) {
            UserModel beanX = item.getUser();
            if (beanX != null){
                GlideUtil.loadImage((ImageView) helper.getView(R.id.iv_item_content_img),beanX.getImgHead(),R.mipmap.placeholder);
            }
        } else {
            GlideUtil.loadImage((ImageView) helper.getView(R.id.iv_item_content_img),item.getImgFace(),R.mipmap.placeholder);
        }

        // 名字与头像 && 性别
        UserModel userBean = item.getUser();
        if (userBean != null) {
            helper.setText(R.id.tv_item_content_name, userBean.getNick());
            ImageView civ_item_content_img = helper.getView(R.id.civ_item_content_img);
            GlideUtil.loadCircleImage(civ_item_content_img, userBean.getImgHead(), R.mipmap.pnl_00_head108);
            if (userBean.getSex() == 1) {
                helper.setImageResource(R.id.iv_item_icon_sex, R.mipmap.ico_00boy);
            } else if (userBean.getSex() == 2) {
                helper.setImageResource(R.id.iv_item_icon_sex, R.mipmap.ico_00girl);
            }
            if (visiableFeed) {
                helper.addOnClickListener(R.id.tv_dynfllow);
                if (userBean.isMyFollow()) {
                    helper.setVisible(R.id.tv_dynfllow, false);
                    helper.setTextColor(R.id.tv_dynfllow, helper.getConvertView().getResources().getColor(R.color.text_b2));
                    helper.setText(R.id.tv_dynfllow, helper.getConvertView().getResources().getString(R.string.has_followed));
                    helper.setBackgroundRes(R.id.tv_dynfllow, R.drawable.shape_corner5_white_bc);
                } else {
                    if (userBean.getUid() == myuid) {
                        helper.setVisible(R.id.tv_dynfllow, false);
                    } else {
                        helper.setVisible(R.id.tv_dynfllow, true);
                    }
                    helper.addOnClickListener(R.id.tv_dynfllow);
                    helper.setTextColor(R.id.tv_dynfllow, helper.getConvertView().getResources().getColor(R.color.color_ff));
                    helper.setText(R.id.tv_dynfllow, helper.getConvertView().getResources().getString(R.string.un_followed));
                    helper.setBackgroundRes(R.id.tv_dynfllow, R.drawable.shape_corner100_white_ff0);
                }
            }

        }


        /**
         * part1
         */
        // 地址
        String city = item.getCity();
        if (TextUtils.isEmpty(city)) {
            city = mContext.getString(R.string.str_dynamics_address);
            helper.setVisible(R.id.tv_item_content_adress_juli, false);
        } else {
            final double longtitude = locationHelper.getLongitude();
            final double latitude = locationHelper.getLatitude();
            if (item.getLongitude() == 0.0 || item.getLatitude() == 0.0 || latitude == 0.0 || longtitude == 0.0) {
                helper.setVisible(R.id.tv_item_content_adress_juli, false);
            } else {
                helper.setVisible(R.id.tv_item_content_adress_juli, true);
                double distance = AppUtil.gps2m(latitude, longtitude, item.getLatitude() / 1000000F, item.getLongitude() / 1000000F);
                distance = distance / 1000D;
                if (distance < 0.5) {
                    distance = 0.5;
                    helper.setText(R.id.tv_item_content_adress_juli, mContext.getString(R.string.str_dynamics_distance, Double.toString(distance)));
                } else if (distance > 0.5 && distance < 10) {
                    distance = changeDouble(distance);
                    helper.setText(R.id.tv_item_content_adress_juli, mContext.getString(R.string.str_dynamics_distance, Double.toString(distance)));
                } else {
                    int d = (int) distance;
                    helper.setText(R.id.tv_item_content_adress_juli, mContext.getString(R.string.str_dynamics_distance, Integer.toString(d)));
                }
            }
        }
        helper.setText(R.id.tv_item_content_adress, city);

        /**
         * 内容
         */
        String des = item.getTitle();
        if (TextUtils.isEmpty(des)) {
            helper.setVisible(R.id.tv_item_dongtai_content_des, false);
        } else {
            helper.setVisible(R.id.tv_item_dongtai_content_des, true);
            helper.setText(R.id.tv_item_dongtai_content_des, des);
        }

        // 时间
        long localTime = System.currentTimeMillis();
        // java乘于1000
        long createTime = item.getTimeStart();
        long diff = (localTime - (createTime * 1000L));
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        helper.setText(R.id.tv_item_content_time, AppUtil.getTimeStr(mContext, days, hours, minutes));

        // 单击事件
        helper.addOnClickListener(R.id.tv_item_content_name);
        helper.addOnClickListener(R.id.civ_item_content_img);
        helper.addOnClickListener(R.id.ll_dynamics_content_live);
        helper.addOnClickListener(R.id.rl_dynamics_content);
    }

    /**
     * 保留一位小数
     *
     * @param dou
     * @return
     */
    public double changeDouble(Double dou) {
        NumberFormat nf = new DecimalFormat("0.0");
        dou = Double.parseDouble(nf.format(dou));
        return dou;
    }
}
